$(function () {
    layui.use(['table','layer','laydate','form'], function(){
        var table = layui.table;
        let layer = layui.layer;
        let form = layui.form;
        let laydate = layui.laydate;
        //第一个实例
        table.render({
            elem: '#demo'
            , toolbar: "#barDemo"
            , height: 'full-100'
            ,url: '/getVaccinator' //数据接口
            ,cols: [[ //表头
                {field: 'sName', title: '受种者姓名', width:100,  fixed: 'left'}
                ,{field: 'sSex', title: '性别', width:70}
                ,{field: 'sMomname', title: '母亲姓名', width:100, sort: true}
                ,{field: 'sMomphone', title: '母亲电话', width:150}
                ,{field: 'sDadname', title: '父亲姓名', width: 80}
                ,{field: 'sDadphone', title: '父亲电话', width: 150, sort: true}
                ,{field: 'sAddress', title: '住址', width: 150}
                ,{field: 'sAppointmenttime', title: '预约时间', width: 140}
                ,{field: 'sVaccines', title: '接种疫苗', width: 155}
                ,{fixed: 'right', title:'操作', toolbar: '#tool', width:100}
            ]]
            ,page: {
                limit:5,
                limits:[1, 5, 10, 40, 100],
                layout:['prev', 'page', 'next','count','limit','refresh']
            } //开启分页
        });
        var $ = layui.$, active = {
            reload: function(){
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        time: 'today'
                    }
                }, 'data');
            }
        };
        $('#today').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        var $1 = layui.$, active1 = {
            reload: function(){
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        time: 'tomorrow'
                    }
                }, 'data');
            }
        };
        $('#tomorrow').on('click', function(){
            var type = $(this).data('type');
            active1[type] ? active1[type].call(this) : '';
        });
        var $2 = layui.$, active2 = {
            reload: function(){
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        time: 'all'
                    }
                }, 'data');
            }
        };
        $('#all').on('click', function(){
            var type = $(this).data('type');
            active2[type] ? active2[type].call(this) : '';
        });
        var $3 = layui.$, active3 = {
            reload: function(){
                var name = $('#search');
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        name: name.val()
                    }
                }, 'data');
            }
        };
        $('#searchbt').on('click', function(){
            var type = $(this).data('type');
            active3[type] ? active3[type].call(this) : '';
        });

        table.on('tool(today)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            if(layEvent === 'manage'){ //查看
                layer.open({
                    type: 1,
                    title: "接种概况",
                    area: ['520px', '350px'],
                    content: '<form class="layui-form"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->\n' +
                        '  <div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">接种日期</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="text" name="date" placeholder="请输入" autocomplete="off" class="layui-input" id="date" style="width: 400px">\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '<div class="layui-form-item">\n' +
                        '    <label class="layui-form-label">是否异常</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <input type="radio" name="yes" value="异常" title="异常" id="yes">\n' +
                        '      <input type="radio" name="yes" value="正常" title="正常" id="no" >\n' +
                        '    </div>\n' +
                        '  </div>' +

                        '<div class="layui-form-item layui-form-text">\n' +
                        '    <label class="layui-form-label">请填写异常描述</label>\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <textarea name="describe" placeholder="请输入内容" class="layui-textarea" style="width: 400px" id="describe"></textarea>\n' +
                        '    </div>\n' +
                        '  </div>' +
                        '<div class="layui-form-item">\n' +
                        '    <div class="layui-input-block">\n' +
                        '      <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>\n' +
                        '      <button type="reset" class="layui-btn layui-btn-primary">重置</button>\n' +
                        '    </div>\n' +
                        '  </div>\n' +
                        '</form>',
                    success: function () {
                        let sNo = data.sNo;
                        let sVaccines = data.sVaccines;
                        form.render();

                        laydate.render({
                            elem: '#date' //指定元素
                        });
                        $.post("/getSituation",{"sNo":sNo},function (data) {
                            console.log(data)
                            if (data.code===500){
                                sessionStorage.removeItem("qNo")
                            }else {
                                $("#date").val(data.qDate)
                                $("#describe").val(data.qReaction)
                                if (data.qYon==="异常"){
                                    $("#yes").attr("checked","checked")
                                    $("#no").removeAttr("checked")
                                    form.render('radio');
                                }else {
                                    $("#no").attr("checked","checked")
                                    $("#yes").removeAttr("checked")
                                    form.render('radio');
                                }
                                let s = JSON.stringify(data);
                                let qNo = data.qNo;
                                window.sessionStorage.setItem("qNo",qNo)
                            }
                        },"json")
                        form.on('submit(*)',function (form) {
                            let item = sessionStorage.getItem("qNo");
                            let parse = JSON.parse(item);
                            if (parse===null){
                                let form1 = form.field;
                                let s = JSON.stringify(form1);
                                console.log(s)
                                $.post("/isSituation",{"form":s,"sNo":sNo},function (data) {
                                    if (data.code===200){
                                        window.location.href="today.html"
                                    }else {
                                        layer.msg(data.msg)
                                        return false
                                    }
                                },"json")
                                return false
                            }
                            layer.msg("不可随意更改")
                            return false;
                        })
                    }
                });
            }
        });
    });
})
